package com.xin.data.simbapesa.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.JsonObject;
import com.xin.data.simbapesa.PathMaping;
import com.xin.data.simbapesa.R;
import com.xin.data.simbapesa.SimbaApp;
import com.xin.data.simbapesa.adapter.AmountAdapter;
import com.xin.data.simbapesa.adapter.ApplyAdapter;
import com.xin.data.simbapesa.analysis.AnalysisUtil;
import com.xin.data.simbapesa.api.PU;
import com.xin.data.simbapesa.api.RxHttp;
import com.xin.data.simbapesa.api.U;
import com.xin.data.simbapesa.base.BaseActivity;
import com.xin.data.simbapesa.base.BaseDialog;
import com.xin.data.simbapesa.base.BaseParams;
import com.xin.data.simbapesa.databinding.SimbaAmountItemBinding;
import com.xin.data.simbapesa.databinding.SimbaApplyActivityBinding;
import com.xin.data.simbapesa.databinding.SimbaApplyDaysItemBinding;
import com.xin.data.simbapesa.dialog.TipsDialog;
import com.xin.data.simbapesa.obj.SimbaApplyDetailInfo;
import com.xin.data.simbapesa.obj.SimbaDetailItemInfo;
import com.xin.data.simbapesa.obj.SimbaScoreInfo;
import com.xin.data.simbapesa.obj.SimbaUserProcess;
import com.xin.data.simbapesa.utils.AESCryptUtilJar;
import com.xin.data.simbapesa.utils.DateUtils;
import com.xin.data.simbapesa.utils.JsonParse;
import com.xin.data.simbapesa.utils.LogUtil;
import com.xin.data.simbapesa.utils.NumUtils;
import com.xin.data.simbapesa.utils.ResUtils;
import com.xin.data.simbapesa.utils.SpUtils;
import com.xin.data.simbapesa.utils.ViewUtils;
import com.xin.data.simbapesa.view.SimbaSpaceItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class SimbaApplyActivity extends BaseActivity<SimbaApplyActivityBinding> {


    int amount;

    int minAmount;

    int maxAmout;


    String days;

    private final int TIME_WHAT = 1;

    private final int TIME_WHAT1 = 2;


    private int timeCout = 60 * 10;


    List<SimbaDetailItemInfo> list1;

    List<SimbaDetailItemInfo> list2;

    ApplyAdapter adater1;

    ApplyAdapter adater2;

    int titleCout;

    TipsDialog mTipsDialog;

    SimbaApplyDetailInfo applyDetail;

    String currency = "";

    AmountAdapter amountAdapter;

    List<View> viewList;


    @Override
    protected SimbaApplyActivityBinding initViewBinding(LayoutInflater inflater) {
        return SimbaApplyActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {


        binding.tvBk.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            finish();
        });
        binding.tvCs.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P33, "");
            startActivity(new Intent(getActivity(), SimbaCustomerServiceActivity.class));
        });
        binding.tvBtn.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P36, "");
            if (SimbaApp.getInstance().isOrder()) {
                showTipsDialog();
            } else {
                apply();
            }
        });


        binding.refresh.setOnRefreshListener(() -> {
            PU.userBuriedPoint(PU.P32, "");
            amount = 0;
            getDetailPageForDays(true, false);
            fetchStarWindowConfig();
        });


        list1 = new ArrayList<>();
        list2 = new ArrayList<>();

        LinearLayoutManager manager1 = new LinearLayoutManager(getActivity());
        manager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView1.setLayoutManager(manager1);
        int d = ResUtils.d(R.dimen.dp_6);
        binding.recyclerView1.addItemDecoration(new SimbaSpaceItemDecoration(d, d, getActivity()));
        adater1 = new ApplyAdapter(list1);
        binding.recyclerView1.setAdapter(adater1);

        LinearLayoutManager manager2 = new LinearLayoutManager(getActivity());
        manager2.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView2.setLayoutManager(manager2);
        binding.recyclerView2.addItemDecoration(new SimbaSpaceItemDecoration(d, d, getActivity()));
        adater2 = new ApplyAdapter(list2);
        binding.recyclerView2.setAdapter(adater2);


        setTipsText(" 00:00");

        binding.getRoot().setPadding(0, 0, 0, 0);

        setStatusBarHeight(binding.tvRoot);


        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @SuppressLint("RtlHardcoded")
            @Override
            public void onPageSelected(int position) {

                int amount2 = amount;

                for (int i = 0; i < viewList.size(); i++) {
                    View view = viewList.get(i);
                    TextView title = view.findViewById(R.id.tvAmount);
                    LogUtil.e("me", i + "<-position->" + position);
                    if (position == i) {
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
                        title.setTextColor(Color.parseColor("#FFFFFF"));
                        title.setGravity(Gravity.CENTER);
                        title.setBackground(ResUtils.de(R.mipmap.amount_bg));
                        amount2 = (int) NumUtils.str2Double((String) title.getTag());
                    } else {
                        title.setBackground(ResUtils.de(android.R.color.transparent));
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                        title.setTextColor(Color.parseColor("#80FFFFFF"));
                        if (i < position) {
                            title.setGravity(Gravity.RIGHT);
                        } else {
                            title.setGravity(Gravity.LEFT);
                        }
                    }
                }

                if (applyDetail != null) {
                    if (amount2 > amount) {
                        PU.userBuriedPoint(PU.P35, "" + amount2);
                    } else {
                        PU.userBuriedPoint(PU.P34, "" + amount2);
                    }
                    LogUtil.e("me", amount + "<-progress->" + amount2);
                    amount = amount2;
                    getDetailPageForDays(false, true);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewList = new ArrayList<>();
        amountAdapter = new AmountAdapter(viewList);
        binding.viewPager.setPageMargin(0);
        binding.viewPager.setAdapter(amountAdapter);


    }

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P29;
        UP_TYPE2 = PU.P30;
        mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
        getDetailPageForDays(false, false);
        fetchStarWindowConfig();
    }


    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == TIME_WHAT) {
                mHandler.removeMessages(TIME_WHAT);
                timeCout = timeCout - 1;
                if (timeCout <= 0) {
                    setTipsText(" 00:00");
                    timeCout = 60 * 10;
                    mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
                } else {
                    String str = DateUtils.convertSecondsToTime(timeCout);
                    setTipsText(" " + str);
                    mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
                }
            } else if (msg.what == TIME_WHAT1) {

            }
        }
    };


    void showTipsDialog() {
        if (mTipsDialog == null) {
            mTipsDialog = new TipsDialog(this);
        }
        String date;
        if (SimbaApp.getInstance().getAppInfo() != null && !TextUtils.isEmpty(SimbaApp.getInstance().getAppInfo().getRejectAfterDays())) {
            date = SimbaApp.getInstance().getAppInfo().getRejectAfterDays();
            int day = NumUtils.str2Int(date);
            if (day > 0) {
                date = DateUtils.getNowDateForDays(day);
            }
        } else {
            date = DateUtils.getNowDateForDays(30);
        }
        mTipsDialog.hideCancel(true);
        mTipsDialog.setContentTxt(ResUtils.s(R.string.apply_hit1, date));
        mTipsDialog.show();
    }

    void setTipsText(String date) {
        binding.tvHit.setText(Html.fromHtml(ResUtils.s(R.string.apply_hit, date)));
    }

    @SuppressLint({"SetTextI18n", "RtlHardcoded"})
    void loadData(boolean fromUser) {

        if (getActivity() != null && !isFinishing()) {


            if (applyDetail != null) {

                binding.tvHit.setVisibility(View.VISIBLE);

                list1.clear();
                list2.clear();
                titleCout = 0;

                amount = applyDetail.getAmount();

                minAmount = applyDetail.getMinAmoun();
                maxAmout = applyDetail.getMaxAmount();

                currency = "";
                if (TextUtils.isEmpty(currency)) {
                    currency = SpUtils.get_string(this, SpUtils.KEY_CURRENCY, "");
                }
                if (TextUtils.isEmpty(currency)) {
                    currency = ResUtils.s(R.string.ghs);
                }


//                maxAmout = 500000;
//                minAmount = 30000;
//                applyDetail.setChooseAmountFlag(true);
//                applyDetail.setGranularity(10000);

                LogUtil.e("me", maxAmout + "<-maxAmout->" + amount);


                if (maxAmout > minAmount && applyDetail.isChooseAmountFlag()) {


                    if (!fromUser) {
                        List<String> datas = new ArrayList<>();
                        int max = (maxAmout - minAmount) / applyDetail.getGranularity();
                        int index = 0;
                        for (int i = 0; i <= max; i++) {
                            String data = minAmount + applyDetail.getGranularity() * i + "";
                            LogUtil.e("me", amount + "<-data amount->" + data);
                            datas.add(data);
                            if (data.equals(String.valueOf(amount))) {
                                index = i;
                            }
                        }

                        viewList.clear();
                        for (int i = 0; i < datas.size(); i++) {
                            SimbaAmountItemBinding binding1 = SimbaAmountItemBinding.inflate(LayoutInflater.from(getActivity()));
                            binding1.tvAmount.setText(datas.get(i));
                            if (index == i) {
                                binding1.tvAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
                                binding1.tvAmount.setTextColor(Color.parseColor("#FFFFFF"));
                                binding1.tvAmount.setGravity(Gravity.CENTER);
                            } else {
                                binding1.tvAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                                binding1.tvAmount.setTextColor(Color.parseColor("#80FFFFFF"));
                                binding1.tvAmount.setBackground(ResUtils.de(android.R.color.transparent));

                                if (i < index) {
                                    binding1.tvAmount.setGravity(Gravity.RIGHT);
                                } else {
                                    binding1.tvAmount.setGravity(Gravity.LEFT);
                                }

                            }
                            binding1.tvAmount.setTag(datas.get(i));
                            viewList.add(binding1.getRoot());
                        }

                        amountAdapter.notifyDataSetChanged();
                        binding.viewPager.setCurrentItem(index);
                        amountAdapter.notifyDataSetChanged();


                    }


                } else {
                    if (!fromUser) {

                        List<String> datas = new ArrayList<>();
                        datas.add(String.valueOf(amount));

                        viewList.clear();
                        for (int i = 0; i < datas.size(); i++) {
                            SimbaAmountItemBinding binding1 = SimbaAmountItemBinding.inflate(LayoutInflater.from(getActivity()));
                            binding1.tvAmount.setText(datas.get(i));
                            binding1.tvAmount.setTag(datas.get(i));
                            viewList.add(binding1.getRoot());
                        }

                        amountAdapter.notifyDataSetChanged();
                        binding.viewPager.setCurrentItem(0);
                        amountAdapter.notifyDataSetChanged();


                    }

                }


                if (applyDetail.getDetailItemList() != null) {

                    for (SimbaDetailItemInfo itemBean : applyDetail.getDetailItemList()) {
                        if (!TextUtils.isEmpty(itemBean.getShowName()) && !TextUtils.isEmpty(itemBean.getShowDetail())) {
                            if (!TextUtils.isEmpty(itemBean.getTitle()) && itemBean.getTitle().equals("YES")) {
                                titleCout = titleCout + 1;
                            }
                            if (titleCout < 2) {
                                list1.add(itemBean);
                            } else {
                                list2.add(itemBean);
                            }
                        }
                    }

                }

//                SimbaDetailItemInfo detailItemInfo1=list1.get(1);
//                detailItemInfo1.setDiscount("20KS");
//                detailItemInfo1.setOldDetail("100KS");
//                detailItemInfo1.setShowDetail("80KS");
//
//                SimbaDetailItemInfo detailItemInfo2=list2.get(1);
//                detailItemInfo2.setDiscount("20KS");
//                detailItemInfo2.setOldDetail("100KS");
//                detailItemInfo2.setShowDetail("80KS");

                adater1.updateAllData();
                adater2.updateAllData();
                days = applyDetail.getDays();


                initTab();


            }


        }

    }


    @SuppressLint("SetTextI18n")
    void initTab() {
        if (applyDetail != null) {
            String[] tabs = applyDetail.getDaysArray();
//            String[] tabs = new String[2];
//            tabs[0] = "120";
//            tabs[1] = "200";
            if (tabs != null && tabs.length > 0) {
                days = applyDetail.getDays();
                LogUtil.e("me", "<-days->" + days);
                binding.tvDayLayout.removeAllViews();
                for (int i = 0; i < tabs.length; i++) {
                    String tab = tabs[i];
                    SimbaApplyDaysItemBinding binding1 = SimbaApplyDaysItemBinding.inflate(LayoutInflater.from(this));
                    binding1.getRoot().setTag(binding1);
                    binding1.tvTitle.setText(tab);
                    binding1.tvTitle.setTag(i);
                    if (tab.equalsIgnoreCase(days)) {
                        binding1.tvTitle.setSelected(true);
                    }
                    if (i == 0) {
                        binding1.tvRoot.setPadding(0, 0, 0, 0);
                    }
                    binding.tvDayLayout.addView(binding1.getRoot());
                    binding1.getRoot().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ViewUtils.viewScale(v);
                            SimbaApplyDaysItemBinding binding2 = (SimbaApplyDaysItemBinding) v.getTag();
                            days = binding2.tvTitle.getText().toString();
                            switchTab();
                        }
                    });
                }
            } else if (!TextUtils.isEmpty(applyDetail.getDays())) {
                binding.tvDayLayout.removeAllViews();
                String tab = applyDetail.getDays();
                days = tab;
                SimbaApplyDaysItemBinding binding1 = SimbaApplyDaysItemBinding.inflate(LayoutInflater.from(this));
                binding1.getRoot().setTag(binding1);
                binding1.tvTitle.setText(tab);
                binding1.tvTitle.setSelected(true);
                binding.tvDayLayout.addView(binding1.getRoot());
            }
        }
    }

    void switchTab() {
        int cout = binding.tvDayLayout.getChildCount();
        for (int i = 0; i < cout; i++) {
            View view = binding.tvDayLayout.getChildAt(i);
            SimbaApplyDaysItemBinding binding1 = (SimbaApplyDaysItemBinding) view.getTag();
            if (binding1.tvTitle.getText().toString().equalsIgnoreCase(days)) {
                binding1.tvTitle.setSelected(true);
            } else {
                binding1.tvTitle.setSelected(false);
            }
        }
        PU.userBuriedPoint(PU.P31, days);
        getDetailPageForDays(false, true);
    }

    void getDetailPageForDays(boolean isRefresh, boolean fromUser) {
        if (binding != null) {
            if (!isRefresh) {
                showLoading("");
            }
            JsonObject json = BaseParams.getJsonObject(getActivity(), PathMaping.PathList[80]);
            json.addProperty("amount", String.valueOf(amount));
            if (!TextUtils.isEmpty(days)) {
                json.addProperty("days", String.valueOf(days));
            }
            json.addProperty("oldFlag", "YES");

            HashMap<String, String> heads = new HashMap<>();
            heads.put("product", U.URL_PRODUCT);
            if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
                heads.put("country", U.URL_COUNTRY);
            }
            String data = AESCryptUtilJar.encryptDefault(json.toString());
            LogUtil.e("me", json + "<-getLoanDetailPageForDays->" + data);
            RxHttp.post(U.commonUrl, data, heads)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<>() {
                        @Override
                        public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                            if (binding != null) {
                                hideLoading();
                                binding.refresh.setRefreshing(false);
                                if (!TextUtils.isEmpty(s)) {
                                    String json1 = AESCryptUtilJar.decryptDefault(s);
                                    LogUtil.e("me", "getLoanDetailPageForDays json->" + json1);
                                    if (JsonParse.getCode(json1)) {
                                        applyDetail = JsonParse.getDetailPageForDaysJson2Obj(json1);
                                        loadData(fromUser);
                                    } else {
                                        showFailedDialog(new BaseDialog.BtnClickListener() {
                                            @Override
                                            public void btnOnClickOk() {
                                                getDetailPageForDays(false, false);
                                                fetchStarWindowConfig();
                                            }

                                            @Override
                                            public void btnOnClickCancel() {

                                            }

                                            @Override
                                            public void btnOnClickColse() {

                                            }
                                        });
                                    }
                                }
                            }
                        }

                        @Override
                        public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                            if (binding != null) {
                                hideLoading();
                                binding.refresh.setRefreshing(false);
                                showFailedDialog(new BaseDialog.BtnClickListener() {
                                    @Override
                                    public void btnOnClickOk() {
                                        getDetailPageForDays(false, false);
                                        fetchStarWindowConfig();
                                    }

                                    @Override
                                    public void btnOnClickCancel() {

                                    }

                                    @Override
                                    public void btnOnClickColse() {

                                    }
                                });
                            }
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    void fetchStarWindowConfig() {
        SimbaUserProcess bean = SimbaApp.getInstance().getProcess();
        if (!SimbaApp.getInstance().isScore() && bean != null && !TextUtils.isEmpty(bean.getUserType()) && bean.getUserType().equals("OLD")) {
            JsonObject json = BaseParams.getJsonObject(getActivity(), PathMaping.PathList[66]);
            HashMap<String, String> heads = new HashMap<>();
            heads.put("product", U.URL_PRODUCT);
            if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
                heads.put("country", U.URL_COUNTRY);
            }
            String data = AESCryptUtilJar.encryptDefault(json.toString());
            LogUtil.e("me", json + "<-fetchStarWindowConfig post->" + data);
            RxHttp.post(U.commonUrl, data, heads)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<>() {
                        @Override
                        public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                            if (binding != null) {
                                hideLoading();
                                if (!TextUtils.isEmpty(s)) {
                                    String json1 = AESCryptUtilJar.decryptDefault(s);
                                    LogUtil.e("me", "fetchStarWindowConfig json->" + json1);
                                    if (JsonParse.getCode(json1)) {
                                        SimbaScoreInfo scoreBean = JsonParse.fetchStarWindowConfigJson2Obj(json1);
                                        if (scoreBean != null) {
                                            int num = SpUtils.get_int(getActivity(), SpUtils.KEY_SCORE_NUM);
                                            if (!TextUtils.isEmpty(scoreBean.getSwitch_data()) && (scoreBean.getSwitch_data().equals("YES") && num < scoreBean.getNum())) {
                                                SimbaApp.getInstance().setScore(true);
                                                num = num + 1;
                                                SpUtils.save_int(getActivity(), SpUtils.KEY_SCORE_NUM, num);
                                                Intent intent = new Intent(getActivity(), SimbaScoreActivity.class);
                                                intent.putExtra("content", scoreBean.getContent());
                                                startActivity(intent);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        @Override
                        public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                            if (binding != null) {
                                hideLoading();
                            }
                            if (e != null) {
                                LogUtil.e("me", "fetchStarWindowConfig e->" + e);
                            }
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    void apply() {

        if (amount <= 0) {
            showToastDialog(R.mipmap.hit_icon, ResUtils.s(R.string.apply_hit2));
            return;
        }


        JsonObject json = BaseParams.getJsonObject(getActivity(), PathMaping.PathList[14]);
        json.addProperty("amount", String.valueOf(amount));
        json.addProperty("days", String.valueOf(days));

        HashMap<String, String> heads = new HashMap<>();
        heads.put("product", U.URL_PRODUCT);
        if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
            heads.put("country", U.URL_COUNTRY);
        }
        String data = AESCryptUtilJar.encryptDefault(json.toString());
        LogUtil.e("me", json + "<-apply->" + data);

        showLoading("");

        RxHttp.post(U.commonUrl, data, heads)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                        if (binding != null) {
                            hideLoading();
                            if (!TextUtils.isEmpty(s)) {
                                String json1 = AESCryptUtilJar.decryptDefault(s);
                                LogUtil.e("me", "apply json->" + json1);
                                String orderId = JsonParse.applyJson2Obj(json1);
                                if (JsonParse.getCode(json1)) {
                                    PU.userBuriedAllPoint(getActivity());
                                    AnalysisUtil.INSTANCE.addApplyEvent();
                                    if (!TextUtils.isEmpty(orderId)) {
                                        Intent intent = new Intent(getActivity(), SimbaTimeActivity.class);
                                        intent.putExtra("orderId", orderId);
                                        startActivity(intent);
                                        finish();
                                    }
                                } else {
                                    String msg = JsonParse.getMsg(getActivity(), json1);
                                    LogUtil.e("me", "apply msg->" + msg);
                                    showToastDialog(R.mipmap.hit_icon, msg);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        if (binding != null) {
                            hideLoading();
                            showToastDialog(R.mipmap.hit_icon, getResources().getString(R.string.start_net_failed));
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeMessages(TIME_WHAT);
            mHandler.removeMessages(TIME_WHAT1);
            mHandler = null;
        }
    }


}
